package cn.yyh.serviceProduct.service.impl;

import cn.yyh.commons.service.impl.MyBaseServiceImpl;
import cn.yyh.commons.utils.BaseQuery;
import cn.yyh.commons.utils.PageInfo;
import cn.yyh.serviceProduct.client.EmpClient;
import cn.yyh.serviceProduct.domain.Employee;
import cn.yyh.serviceProduct.domain.Product;
import cn.yyh.serviceProduct.domain.ProductDetail;
import cn.yyh.serviceProduct.mapper.ProductDetailMapper;
import cn.yyh.serviceProduct.mapper.ProductMapper;
import cn.yyh.serviceProduct.query.ProductQuery;
import cn.yyh.serviceProduct.service.ProductService;
import cn.yyh.serviceProduct.util.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: ProductServiceImpl
 * @Package: cn.yyh.serviceProduct.service.impl
 * @date 2021/7/12
 */
@Service
public class ProductServiceImpl extends MyBaseServiceImpl<ProductMapper, Product> implements ProductService {
   @Autowired
   private EmpClient empClient;
   @Autowired
   private ProductDetailMapper detailMapper;


    @Override
    public PageInfo<Product> pageQuery(ProductQuery query, Long id, String token) {
        String infoId = empClient.getByLoginInfoId(id, token);
        Employee employee = ConvertUtils.toEmp(ConvertUtils.emp(infoId));
        if (employee != null) {
            if (!ObjectUtils.isEmpty(employee.getShopId())) {
                query.setShopId(employee.getShopId());
            }
        }
        Integer count = baseMapper.countByQuery(query);
        List<Product> products = baseMapper.pageQuery(query);
        PageInfo<Product> info = new PageInfo<>();
        info.setRows(products);
        info.setTotals(count);
        return info;
    }

    @Override
    public void onSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onsaletime", new Date());
        baseMapper.onSale(map);
    }

    @Override
    public void offSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offsaletime", new Date());
        baseMapper.offSale(map);
    }

    @Override
    public void save(Product product,Long id, String token) {
        String infoId = empClient.getByLoginInfoId(id, token);
        Employee employee = ConvertUtils.toEmp(ConvertUtils.emp(infoId));
        if (employee != null) {
            if (!ObjectUtils.isEmpty(employee.getShopId())) {
                product.setShopId(employee.getShopId());
            }
        }
        super.save(product);
        ProductDetail detail = product.getDetail();
        if (detail!=null){
            detail.setProduct_id(product.getId());
            detailMapper.save(detail);
        }
    }

    @Override
    public void update(Product product,Long id, String token) {
        String infoId = empClient.getByLoginInfoId(id, token);
        Employee employee = ConvertUtils.toEmp(ConvertUtils.emp(infoId));
        if (employee != null) {
            if (!ObjectUtils.isEmpty(employee.getShopId())) {
                product.setShopId(employee.getShopId());
            }
        }
        super.update(product);
        ProductDetail detail = product.getDetail();
        if (detail!=null){
            if (detail.getId()!=null){
                //有详情
                detailMapper.update(detail);
            }else{
                detailMapper.save(detail);
            }
        }
    }

    @Override
    public void delete(Long id) {
        detailMapper.deleteByProduct(id);
        super.delete(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        detailMapper.deleteBatchByProduct(ids);
        super.deleteBatch(ids);
    }
}
